import { getSetting,openSetting,chooseAddress,showModal,showToast, requestPayment } from "../../utils/asyncWx.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
import {request} from "../../request/index.js";

// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    cart:[],
    
    totalPrice:0,
    totalNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


 onShow(){
    //获取缓存中的数据
    const address = wx.getStorageSync("address");
    //获取缓存中的购物车数据
    let cart = wx.getStorageSync("cart") || [];
    //过滤后的购物车
    cart = cart.filter(v=>v.checked);


    // //计算全选
    // // const allChecked = cart.length?cart.every(v => v.checked):false;
    // let allChecked = true;
    // //总价格 总数量
    // let totalPrice = 0;
    // let totalNum = 0;
    // cart.forEach(v => {
    //   if(v.checked){
    //     totalPrice += v.num * v.goods_price;
    //     totalNum += v.num;
    //   }else{
    //     allChecked = false;
    //   }
    // });
    // //判断数组是否为空
    // allChecked = cart.length!=0?allChecked:false;
    // //给data赋值
    // this.setData({
    //   address,
    //   cart,
    //   allChecked,
    //   totalPrice,
    //   totalNum
    // })

    this.setData({
      address
    })
    //总价格 总数量
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
     
      totalPrice += v.num * v.goods_price;
      totalNum += v.num;
      
    });
    

     
     this.setData({
      cart,
      totalPrice,
      totalNum,
      address
    })
      
  },


//点击支付
async handleOrderPay(){
  
  try {
    //判断是否有token
    const token = wx.getStorageSync("token");
    
    //判断
    if(!token){
      wx.navigateTo({
        url: '/pages/auth/index'
      });
      return ;
    }
    //创建订单
    //准备 请求头参数
    const header = {Authorization:token};
    //准备 请求题参数
    const order_price = this.data.totalPrice;
    const consignee_addr = this.data.address.all;
    let goods = [];
    const cart = tgis.data.cart;
    cart.forEach(v=>goods.push({
      goods_id:v.goods_id,
      goods_number:v.goods_number,
      goods_price:v.goods_price
    }));
    const orderParams = {order_price,consignee_addr,goods};
    //发送请求 创建订单 获取订单编号
    const {order_number} = await request({url:"https://api-hmugo-web.itheima.net/api/public/v1/my/orders/create",methods:"POST",data:orderParams,header:header});
    //发起预支付
    const {pay} = await request({url:"https://api-hmugo-web.itheima.net/api/public/v1/my/orders/req_unifiedorder",methods:"POST",header,data:{order_number}});
    //发起微信支付
    await  requestPayment(pay);
    //查询后台 订单状态
    const res = await request({url:"https://api-hmugo-web.itheima.net/api/public/v1/my/orders/chkOrder",methods:"POST",header,data:{order_number}}); 

    await showToast({title:"支付成功"});
    //手动清除缓存中的数据
    let  newCart = wx.getStorageSync("cart");
    newCart = newCart.filter(v=>!v.checked);
    wx.setStorageSync("cart", newCart);
      

    wx.navigateTo({
      url:"/pages/order/index"
    })
  } catch (error) {

    await showToast({title:"支付失败"});
    console.log(error);
  }
    
}






 
  
})

